﻿using GenericHtml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WLFramework.Web;
using WLFramework.Data;
using WLFramework.Data.Linq;
using WLFramework.Json;
using WLFramework.DataAccess;
using WLFramework.Validation;

namespace SampleSite.admin.ajax.tree
{
    /// <summary>
    /// getMenusByApp 的摘要说明
    /// </summary>
    public class getMenusByApp : AjaxHttpHandler
    {
        private class TreeAttributes
        {
            /// <summary>
            /// 1: app 2: group 3: menu
            /// </summary>
            public int type { get; set; }

            public int group_id { get; set; }
        }

        private class TreeNode
        {
            public int id { get; set; }
            public string text { get; set; }
            public bool @checked { get; set; }
            
            public TreeAttributes attributes { get; set; }
            public List<TreeNode> children { get; set; }
        }

        private class InputData
        {
            [Required]
            public int a_id { get; set; }

            [Required]
            public int r_id { get; set; }
        }

        public override void OnProcessRequest(HttpContext context)
        {
            InputData input;
            var errors = context.GetModel<InputData>(out input);
            if (errors.Count > 0)
                throw new Exception("参数错误");

            using (CommandHelper db = new CommandHelper(CommandMethod.Read))
            {
                var app = db.Context.sys_RoleApplication
                            .Where(k => k.A_ID == input.a_id &&
                                        k.R_ID == input.r_id &&
                                        k.A_State == 1)
                            .Select(k => k.sysRoleApplicationsysApplications)
                            .First();

                var rm = from p in db.Context.sys_RoleModule
                         where p.R_ID == input.r_id && p.M_State == 1
                         select p;

                var query = from p in db.Context.sys_ModuleList
                            join r in rm on p.M_ID equals r.M_ID
                                into temp
                            from t in temp.DefaultIfEmpty()
                            where p.M_State == 1 &&
                                  p.M_AppID == input.a_id
                            select new
                            {
                                p.M_ID,
                                p.M_CName,
                                p.M_ParentID,
                                State = t == null ? false : true
                            };

                var modules = query.ToList();
                var list = new List<TreeNode>();
                list.Add(new TreeNode()
                {
                    id = -input.a_id,
                    text = app.A_CName,
                    attributes = new TreeAttributes { type = 1 },
                    children = (from m in modules
                                where m.M_ParentID == 0
                                select new TreeNode
                                {
                                    id = m.M_ID,
                                    attributes = new TreeAttributes { type = 2 },
                                    text = m.M_CName,
                                    children = (from p in modules
                                                where p.M_ParentID == m.M_ID
                                                select new TreeNode
                                                {
                                                    id = p.M_ID,
                                                    attributes = new TreeAttributes
                                                    {
                                                        type = 3,
                                                        group_id = p.M_ParentID
                                                    },
                                                    text = p.M_CName,
                                                    @checked = p.State
                                                }).ToList()
                                }).ToList()
                });

                context.Response.Write(JSON.Instance.ToJSON(list));
            }
        }
    }
}